// pages/messages/messages.js
import Toast from '@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeNames: ['1'],
    count:0,
    msgCount:0,
    active: 1,
    elderIds:[],
    nurseId:'1',
    list_num:[],
    bedNumber:'',
    ids:[],
    recordList:[],
    currentTime:'',
    messageList:
    [
      {
        time:'00:00:00',
        messageDetails:
        [
          
        ]
      },
      {
        time:'01:00:00',
        messageDetails:[]
      },
      {
        time:'02:00:00',
        messageDetails:[]
      },
      {
        time:'03:00:00',
        messageDetails:[]
      },
      {
        time:'04:00:00',
        messageDetails:[]
      },
      {
        time:'05:00:00',
        messageDetails:[]
      },
      {
        time:'06:00:00',
        messageDetails:[]
      },
      {
        time:'07:00:00',
        messageDetails:[]
      },
      {
        time:'08:00:00',
        messageDetails:
        [
        ]
      },
      {
        time:'09:00:00',
        messageDetails:[]
      },
      {
        time:'10:00:00',
        messageDetails:[]
      },
      {
        time:'11:00:00',
        messageDetails:[]
      },
      {
        time:'12:00:00',
        messageDetails:[]
      },
      {
        time:'13:00:00',
        messageDetails:[]
      },
      {
        time:'14:00:00',
        messageDetails:[]
      },
      {
        time:'15:00:00',
        messageDetails:[]
      },
      {
        time:'16:00:00',
        messageDetails:[]
      },
      {
        time:'17:00:00',
        messageDetails:[]
      },
      {
        time:'18:00:00',
        messageDetails:[]
      },
      {
        time:'19:00:00',
        messageDetails:[]
      },
      {
        time:'20:00:00',
        messageDetails:[]
      },
      {
        time:'21:00:00',
        messageDetails:[]
      },
      {
        time:'22:00:00',
        messageDetails:[]
      },
      {
        time:'23:00:00',
        messageDetails:[]
      },
    ]
  },
  onChange(event) {
    console.log(event.detail)
    this.setData({
      active:event.detail
    })
  },
  onChange1(event) {
    this.setData({
      activeNames: event.detail,
    });
  },

  //跳转录药
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
   
    var date = new Date()
    var time = date.getHours()
    var minute = date.getMinutes()
    if(time<10){
      time = `0${time}`
    }
    if(minute<10){
      minute = `0${minute}`
    }
    var time1 = `${time}:${minute}:00`
    this.setData({
      currentTime:time1
    })
    const that = this
  },
  createRecord(){
    wx.request({
      url: 'https://zhixiao.site:8080/medication-record/generate',
      success:(res)=>{
      },
      complete:(res)=>{
        this.queryElders()
      }
    })
  },
  queryElders(){
    wx.request({
      url: 'https://zhixiao.site:8080/client/names',
      data:{
        id:wx.getStorageSync('userid')
      },
      success:(res)=>{
        console.log(res)
      },
      complete:(res)=>{
        if(res.data.data===null){
          Toast('今日无用药任务')
          return
        }
        console.log(res.data.data)
        var elders = res.data.data
        var elderIds = []
        for(var i in elders){
          elderIds.push(Object.keys(elders[i])[0])
        }
        console.log("护工对应所有老人id")
        
        console.log(elderIds)
        this.setData({
          elderIds:elderIds
        })
        for (var i in this.data.elderIds){
          this.queryMedicineList(this.data.elderIds[i])
        }
       
      }
    })
  },
  queryMedicineList(elderId){
    wx.request({
      url: 'https://zhixiao.site:8080/client/mlist',
      data:{
        elderId:elderId,
        nurseId:wx.getStorageSync('userid')
      },
      success:(res)=>{
      },
      complete:(res)=>{
        var medicineList = res.data.data
        console.log(111)
        console.log(medicineList)
        var list_num = new Set()
        var ids = []
        for (var i in medicineList){
          var listId = medicineList[i].id
          console.log(listId)
          list_num.add(listId)
        }
        var list_num1 = Array.from(list_num)
        this.setData({
          list_num:list_num1
        })
   
        if(this.data.list_num.length===0){
          return
        }
        
        this.queryDetails()
      }
    })
  },
  
  queryDetails(){
    wx.request({
      url: `https://zhixiao.site:8080/client/details?ids=${this.data.list_num}`,
      method:'POST',
      header:{
        "Content-Type": "application/json"
      },
      success:(res)=>{
        console.log(999)
        console.log(res)
        var recordList = this.data.recordList
        var recordList1 = res.data.data
        recordList.push(...recordList1)
        this.setData({
          recordList:recordList,
        })
        // this.my_sort()
        var messageList = this.data.messageList
        for (var i in recordList1){
          var index = parseInt(recordList1[i].time.slice(0,2))
          console.log(index)
          messageList[index].messageDetails.push(recordList1[i])
        }
        this.setData({
          messageList:messageList
        })
        console.log(recordList)
      },
      complete:(res)=>{
        var count = this.data.count
         count += 1
         this.setData({
           count:count
         })
         if(count == this.data.elderIds.length){
            console.log("hahahha")
            var date = new Date()
            var time = date.getHours()
            var minute = date.getMinutes()
            if(time<10){
              time = `0${time}`
            }
            if(minute<10){
              minute = `0${minute}`
            }
            var time1 = `${time}:${minute}:00`
            this.setData({
              currentTime:time1
            })
            var recordList = this.data.recordList
           
            for(var i=0;i<recordList.length;i++){
              if(recordList[i].time<=this.data.currentTime){
                var msgCount = this.data.msgCount + 1
                this.setData({
                  msgCount:msgCount
                })
              }
            }
            wx.setStorageSync('msgCount', msgCount)
         }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      count:0,
      msgCount:0
    })
    this.queryElders()
    wx.hideHomeButton()

    var messageList = this.data.messageList
    for(var i in messageList){
      messageList[i].messageDetails = []
    }
    this.setData({
      recordList:[],
      messageList:messageList
    })
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})

